//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Windows;
using System.Windows.Controls;

namespace OpenLS.UI
{
    public class GroupPanel: StackPanel
    {
        static GroupPanel()
        {
            OrientationProperty.OverrideMetadata(typeof(GroupPanel), new 
                FrameworkPropertyMetadata(Orientation.Horizontal,FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));
        }


        protected override Size MeasureOverride(Size availableSize)
        {
            //\\Debug.WriteLine("MeasureOverride " + GetType());

            var result = new Size();
            var sizeToUse = new Size(availableSize.Width + 1, availableSize.Height);
            var vf = new List<VariantFor>();
            foreach (UIElement element in InternalChildren)
            {
                var group = element as Group;
                if (group == null)
                    continue;
                RibbonElement.SetGroupState(group, GroupState.Large);
                List<GroupVariant> c = group.GetGroupVariants();
                bool canAddCollapsed = group.CollapsedStatePriority.IsEnabled;
                foreach (GroupVariant v in c)
                {
                    if (v.State == GroupState.Collapsed)
                        canAddCollapsed = false;
                    vf.Add(new VariantFor(v, group));
                }
                if (canAddCollapsed)
                    vf.Add(new VariantFor(new GroupVariant(double.MaxValue, GroupState.Collapsed), group));
            }
            foreach (UIElement element in InternalChildren)
            {
                element.Measure(sizeToUse);
                result.Width += element.DesiredSize.Width;
                result.Height = Math.Max(result.Height, element.DesiredSize.Height);
            }
            vf.Sort(new Comparison<VariantFor>(VariantFor.Compare));
            foreach (VariantFor vv in vf)
            {
                if (result.Width <= availableSize.Width)
                    break;
                var oldWidth = vv.Group.DesiredSize.Width;
                result.Width -= oldWidth;
                RibbonElement.SetGroupState(vv.Group, vv.Variant.State);
                vv.Group.Measure(sizeToUse);
                var newWidth = vv.Group.DesiredSize.Width;
                //Contract.Assert(newWidth <= oldWidth);
                result.Width += newWidth;
            }
            return result;
        }
        protected override Size ArrangeOverride(Size arrangeSize)
        {
            //\\  Debug.WriteLine("ArrangeOverride " + GetType());

            return base.ArrangeOverride(arrangeSize);
        }

        class VariantFor
        {
            public VariantFor(GroupVariant variant, Group group)
            {
                _variant = variant;
                _group = group;
            }
            public static int Compare(VariantFor v1, VariantFor v2)
            {
                return v1.Variant.Priority.CompareTo(v2.Variant.Priority);
            }

            private readonly GroupVariant _variant;
            public GroupVariant Variant
            {
                get { return _variant; }
            }
            private readonly Group _group;

            public Group Group
            {
                get { return _group; }
            }
	
	

        }
    }
}
